#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
//void qsort(void* base, size_t nitems, size_t size, int (*compar)(const void*, const void*))

typedef struct stu
{
	char name[20];
	int age;
}stu;

void print_arr(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}


int int_cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

int cmp_stu_by_name(const void* p1, const void* p2)
{
	return (*(stu*)p1).name -  (*((stu*)p2)).name;
}

void swap(char* p1, char* p2, size_t size)
{
	for (int i = 0; i < size; i++)
	{
		char temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++;
		p2++;
	}
}

void bubble_sort(void* base, size_t num, size_t size, int (*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int  j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)base + j * size,(char*)base + (j + 1) * size) < 0)
			{
				swap((char*)base + j * size, (char*)base + (j + 1) * size,size);
			}
		}
	}
}

void test1()
{
	int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	print_arr(arr, sz);
	bubble_sort(arr, sz, sizeof(arr[0]), int_cmp);
	print_arr(arr, sz);
}

void test2()
{
	stu arr[] = { {"zhangsan", 15}, {"wangwu", 19}, {"lisi",21} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), cmp_stu_by_name);
}

int main()
{
	test1();
	test2();
	return 0;
}